/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-27 下午03:07:01
 */
package cn.com.ccxe.dao.impl;

import java.util.List;

import cn.com.ccxe.core.entity.CrawlExpression;
import cn.com.ccxe.dao.DBHandler;
import cn.com.ccxe.dao.CrawlExpressionDao;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.parser.CrawlExpressionParser;

/**
 *	
 * @author hetao
 */
public class CrawlExpressionDaoImpl extends DBHandler implements CrawlExpressionDao {

	private static final String SELECT_EXPR = 
		" SELECT " +
			" T.MODULE_ID moduleId," +
			" T.ARTICLE_LIST_EXPRE articleListExpre, " +
			" T.LIST_PAGE_EXPRE listPageExpre, " +
			" DATE_PATTERN datePattern, " +
			" DATE_EXPRE dateExpre " +
		" FROM C_LIST_PAGE T ";

	public OperationStatus add(CrawlExpression listPage) {
		String sql = 
			" INSERT INTO " +
			" 	C_LIST_PAGE (MODULE_ID, ARTICLE_LIST_EXPRE, LIST_PAGE_EXPRE, DATE_PATTERN, DATE_EXPRE) "+
			" VALUES (?, ?, ?, ?, ?)";
		
		Object[] parameters = new Object[]{
				listPage.getModuleId(),
				listPage.getArticleListExpre(),
				listPage.getListPageExpre(),
				listPage.getDatePattern(),
				listPage.getDateExpre()
		};
		return insertOrUpdateOrDelete(sql, parameters);
	}

	public OperationStatus del(int id) {
		String sql = "DELETE C_LIST_PAGE WHERE MODULE_ID = ? ";
		return insertOrUpdateOrDelete(sql,new Object[]{id});
	}

	public CrawlExpression get(int websiteId) {
		StringBuilder sb = new StringBuilder(SELECT_EXPR);
		sb.append("WHERE MODULE_ID = ?");
		return queryObject(sb.toString(), new Object[]{websiteId}, new CrawlExpressionParser());
	}

	public OperationStatus update(CrawlExpression listPage) {
		String sql = 
				" UPDATE C_LIST_PAGE T " +
				" SET " +
					"T.ARTICLE_LIST_EXPRE = ?," +
					"T.LIST_PAGE_EXPRE = ?," +
					"T.DATE_PATTERN = ?" +
					"T.DATE_EXPRE = ?" +
				" WHERE T.MODULE_ID = ? ";
		
		Object[] parameters = new Object[]{
				listPage.getArticleListExpre(),
				listPage.getListPageExpre(),
				listPage.getDatePattern(),
				listPage.getDateExpre(),
				listPage.getModuleId()
		};
		return insertOrUpdateOrDelete(sql, parameters);
	}

	public List<CrawlExpression> getAll() {
		return queryList(SELECT_EXPR, null, new CrawlExpressionParser());
	}
	
	public static void main(String[] args) {
		System.out.println(SELECT_EXPR);
	}
	
}
